<?php
class category extends core {
	public $goods_mod;
	
	public function __construct() {
		parent::__construct();
		$this->goods_mod = m('goods');
	}

	//index
	public function index() {
		$where = '';
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$parent_id = (isset($_GET['parent_id']) && trim($_GET['parent_id'])) ? intval($_GET['parent_id']) : 0;
		$all = (isset($_GET['all']) && trim($_GET['all'])) ? intval($_GET['all']) : 0; //显示所有分类
		if ($id) {
			$where .= "AND id='{$id}'";
		}
		if ($keyword) {
			$where .= "AND (name LIKE '%{$keyword}%' OR keywords LIKE '%{$keyword}%')";
		}
		if (!$all) {
			$where .= " AND parent_id='{$parent_id}'";
		}
		//分页
		$this->setpages(array('id'=>$id, 'keyword'=>$keyword, 'parent_id'=>$parent_id, 'all'=>$all));

		$sql = "SELECT *, '' as parent_name, NULL as children, NULL as parents FROM {$this->tbp}goods_category WHERE 1 {$where} ORDER BY sort ASC, id ASC";
		$rs = $this->ezr->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $g) {
				if ($g['parent_id']>0) {
					$sql = "SELECT name FROM {$this->tbp}goods_category WHERE id='".$g['parent_id']."'";
					$parent_name = $this->db->get_var($sql);
					$rs[$key]['parent_name'] = $parent_name;
				}
				
				$sql = "SELECT id, name FROM {$this->tbp}goods_category WHERE parent_id='".$g['id']."'";
				$rs[$key]['children'] = $this->db->get_results($sql);
				
				$ids = $this->goods_mod->get_category_parents_tree($g['id']);
				$sql = "SELECT id, name FROM {$this->tbp}goods_category WHERE id IN ({$ids})";
				$rs[$key]['parents'] = $this->db->get_results($sql);
			}
		}
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('id',$id);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('parent_id',$parent_id);
		$this->smarty->assign('all',$all);
		$categorys = $this->goods_mod->get_categorys();
		$this->smarty->assign('categorys', $this->goods_mod->set_categorys_option($categorys, $parent_id));
		$this->display();
	}

	public function add() {
		if (IS_POST) {
			$this->save();
		} else {
			$categorys = $this->goods_mod->get_categorys();
			$this->smarty->assign('categorys', $this->goods_mod->set_categorys_option($categorys));
			$this->display();
		}
	}

	//add
	public function edit() {
		if (IS_POST) { //添加
			$this->save();
		} else { //显示
			$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
			$sql = "SELECT * FROM {$this->tbp}goods_category WHERE id='{$id}'";
			$row = $this->db->get_row($sql);
			$this->smarty->assign('row', $row);
			$categorys = $this->goods_mod->get_categorys();
			$this->smarty->assign('categorys', $this->goods_mod->set_categorys_option($categorys, $row->parent_id));
			$this->display();
		}
	}

	//写入到数据库
	public function save() {
		$id = (isset($_POST['id']) && trim($_POST['id'])) ? intval($_POST['id']) : 0;
		$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : 0;
		$memo = (isset($_POST['memo']) && trim($_POST['memo'])) ? trim($_POST['memo']) : '';
		$keywords = (isset($_POST['keywords']) && trim($_POST['keywords'])) ? trim($_POST['keywords']) : '';
		$status = (isset($_POST['status']) && intval($_POST['status'])) ? intval($_POST['status']) : 0;
		$sort = (isset($_POST['sort']) && intval($_POST['sort'])) ? intval($_POST['sort']) : 0;
		$parent_id = (isset($_POST['parent_id']) && intval($_POST['parent_id'])) ? intval($_POST['parent_id']) : 0;
		$pic = upload_one_file('pic','pic');
		if (!$pic) $pic = (isset($_POST['origin_pic']) && trim($_POST['origin_pic'])) ? trim($_POST['origin_pic']) : '';
		if ($id > 0) {  //edit
			$sql = "UPDATE {$this->tbp}goods_category SET name='{$name}', memo='{$memo}', keywords='{$keywords}', 
				status='{$status}', sort='{$sort}', parent_id='{$parent_id}' 
				WHERE id='{$id}'";
			$this->db->query($sql);
			//是否更改图片
			if ($pic) {
				$sql = "UPDATE {$this->tbp}goods_category SET pic= '{$pic}' WHERE id='{$id}'";
				$this->db->query($sql);
			}
		} else { //add
			$sql = "INSERT INTO {$this->tbp}goods_category(name, memo, keywords, status, sort, pic, parent_id)
				VALUES('{$name}', '{$memo}', '{$keywords}', '{$status}', '{$sort}', '{$pic}', '{$parent_id}')";
			$this->db->query($sql);
			$id = $this->db->insert_id;
		}
		header("Location:?app=category&act=index");
		exit;
	}

	//delete
	public function delete() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$sql = "DELETE FROM {$this->tbp}goods_category WHERE id='{$id}'";
		$this->db->query($sql);
		header("Location:?app=category&act=index");
		exit;
	}
}
